<template>
  <view class="video-modal" @click="closeVideoModal">
    <view class="video-modal-content" @click.stop>
      <video
        :src="videoSrc"
        class="video-player"
        :controls="true"
        :autoplay="true"
        :show-fullscreen-btn="true"
        :show-play-btn="true"
        :show-center-play-btn="true"
        :enable-progress-gesture="false"
        :object-fit="'contain'"
        playsinline="true"
        webkit-playsinline="true"
        x5-playsinline="true"
        @play="onVideoPlay"
        @error="onVideoError"
        :id="'videoPlayer'"
      ></video>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'
import { likeShortVideo, unlikeShortVideo, createShortVideoComment } from '@/api/video/index.ts'
import { showToast, formatDate } from '@/utils/index.ts'

const props = defineProps({
  video: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['close', 'like', 'comment'])

// 处理视频源，尝试使用HTTP协议作为备选方案
const videoSrc = computed(() => {
  let url = props.video.video_file?.fileurl || ''
  // 如果是HTTPS自签名证书，尝试提供HTTP备选方案
  if (url.startsWith('https://')) {
    // 在开发环境中可以考虑使用HTTP作为备选
    // 注意：这只应在开发环境中使用，生产环境应始终使用HTTPS
    console.log('检测到HTTPS视频源，如果播放失败，请检查SSL证书有效性')
  }
  return url
})

const newComment = ref('')

// 处理点赞/取消点赞
const handleLike = async () => {
  try {
    if (props.video.is_liked) {
      // 取消点赞
      const res = await unlikeShortVideo({ video_id: props.video.video_id })
      if (res.code === 200) {
        emit('like', { liked: false, count: props.video.like_count - 1 })
        showToast('已取消点赞')
      }
    } else {
      // 点赞
      const res = await likeShortVideo({ video_id: props.video.video_id })
      if (res.code === 200) {
        emit('like', { liked: true, count: props.video.like_count + 1 })
        showToast('点赞成功')
      }
    }
  } catch (err) {
    console.error('操作失败:', err)
  }
}

// 发表评论
const handleComment = async () => {
  if (!newComment.value.trim()) {
    showToast('请输入评论内容')
    return
  }

  try {
    const res = await createShortVideoComment({
      video_id: props.video.video_id,
      content: newComment.value
    })

    // 添加新评论
    const newCommentData = res.data
    newComment.value = '' // 清空输入框

    emit('comment', newCommentData)
    showToast('评论成功')
  } catch (err) {
    console.error('评论失败:', err)
  }
}

// 关闭视频模态框
const closeVideoModal = () => {
  //#ifdef MP-WEIXIN
  // 退出全屏
  const videoContext = uni.createVideoContext('videoPlayer')
  videoContext.exitFullScreen()
  //#endif
  emit('close')
}

// 视频开始播放事件
const onVideoPlay = (e) => {
  console.log('视频开始播放', e)
}

// 视频播放错误事件
const onVideoError = (e) => {
  console.error('视频播放错误:', e)
  // 提供更详细的错误信息
  let errorMsg = '视频播放失败'
  if (e && e.detail) {
    if (e.detail.errMsg) {
      errorMsg = e.detail.errMsg
    } else if (e.detail.errCode) {
      errorMsg = `错误代码: ${e.detail.errCode}`
    }
  }
  
  uni.showToast({
    title: errorMsg,
    icon: 'none'
  })
  
  // 如果是SSL证书问题，建议检查证书
  if (videoSrc.value.includes('https://')) {
    console.warn('视频播放失败可能与SSL证书有关，请确保使用受信任的SSL证书')
  }
}
</script>

<style scoped lang="scss">
.video-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.video-modal-content {
  width: 90%;
  max-width: 700px;
  background: #000;
  border-radius: 20rpx;
  overflow: hidden;
  position: relative;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.3);
}

.video-player {
  width: 100%;
  height: 420rpx;
  background-color: #000;
}

.video-details {
  padding: 25rpx;
  flex: 1;
  overflow-y: auto;
  background-color: #fafafa;
}

.video-header {
  margin-bottom: 20rpx;
  border-bottom: 2rpx solid #eee;
}

.detail-video-title {
  font-size: 30rpx;
  font-weight: bold;
  display: block;
  margin-bottom: 12rpx;
  color: #333;
}

.detail-video-meta {
  display: flex;
  justify-content: space-between;
}

.detail-meta-text {
  font-size: 28rpx;
  color: #666;
}

.video-description {
  margin-bottom: 20rpx;
  padding: 20rpx;
  background: white;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.description-text {
  font-size: 22rpx;
  color: #666;
  line-height: 1.5;
}

.video-stats-and-actions {
  margin-bottom: 25rpx;
  padding: 20rpx;
  background: white;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.stats-container {
  display: flex;
  justify-content: space-around;
  gap: 20rpx;
  padding-bottom: 10rpx;
}

.stats-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 26rpx;
  cursor: pointer;
}

.stats-icon {
  width: 48rpx;
  height: 48rpx;
}

.video-stats-and-actions .stats-text {
  color: #a1a1a1;
  font-size: 26rpx;
  font-weight: 500;
}

.comment-area {
  background-color: white;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.comments-list {
  max-height: 320rpx;
  padding: 20rpx;
}

.comment-item {
  display: flex;
  margin-bottom: 25rpx;
  padding: 15rpx;
  background-color: #fafafa;
  border-radius: 12rpx;
}

.comment-avatar {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
  margin-right: 20rpx;
  flex-shrink: 0;
}

.comment-content {
  flex: 1;
}

.comment-user {
  font-weight: bold;
  font-size: 28rpx;
  margin-bottom: 8rpx;
  color: #333;
}

.comment-text {
  font-size: 28rpx;
  line-height: 1.5;
  margin-bottom: 8rpx;
  color: #555;
}

.comment-meta {
  display: flex;
  font-size: 24rpx;
  color: #999;
}

.no-comments {
  text-align: center;
  padding: 40rpx;
  font-size: 28rpx;
  color: #999;
}

.comment-input-container {
  display: flex;
  align-items: center;
  background-color: #f8f8f8;
  border-top: 1rpx solid #eee;
  padding: 10rpx;
}

.comment-input {
  flex: 1;
  padding: 15rpx 20rpx;
  font-size: 28rpx;
  background-color: white;
  border: 1rpx solid #eee;
  border-radius: 40rpx;
  outline: none;
  min-height: 40rpx;
  line-height: 40rpx;
  resize: none;
  width: 100%;
}

.send-button {
  font-size: 28rpx;
  padding: 15rpx 30rpx;
  background: linear-gradient(90deg, #ff7e5f, #feb47b);
  color: white;
  border-radius: 40rpx;
  border: none;
  box-shadow: 0 4rpx 12rpx rgba(255, 126, 95, 0.4);
  height: auto;
  line-height: normal;
  margin-left: 15rpx;
  font-weight: 500;
}

.close-button {
  position: absolute;
  top: 20rpx;
  right: 20rpx;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  width: 50rpx;
  height: 50rpx;
  border-radius: 50%;
  font-size: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
</style>